import 'dart:math';

import 'package:easy_rich_text/easy_rich_text.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/routers/jump_router.dart';

///帮助反馈
class MineQAPage extends StatefulWidget {
  static const String routeName = '/question';
  @override
  State<StatefulWidget> createState() => _MineQAPageState();
}

class _MineQAPageState extends State<MineQAPage> {
  final String tg = 'https://t.me/mu02guang';
  final String email = 'xzhan5555@gmail.com';
  final String url = 'XV9.FM';

  late final questionList = [
    {"question": "描述文件安装失败?", "answer": "若提示【新的MDM有效负载与旧的有效负载不匹配】，请移除移动设备管理步骤：【设置-通用-设备管理-移动设备管理-移除管理】", 'isOpen': true},
    {
      "question": "怎么找回账号?",
      "answer":
          "本平台登录会自动创建账号，需保存账号凭证或绑定手机号码，才能记录之前的账号信息。受行业限制，APP无法正常使用时需升级，未绑定手机号码会导致账号信息丢失。请及时绑定手机号码或保存账号凭证，以免VIP信息丢失，造成巨大财产损失！账号丢失的用户可在 『我的』页面-账号找回，原账号的VIP信息会转移至新账号上。『账号凭证』『手机绑定』都没有的情况下，如账号VIP信息丢失，可以提供VIP支付充值凭证截图联系在线客服为您查询核实恢复VIP。",
      'isOpen': false
    },
    {
      "question": "怎么支付不成功?",
      "answer":
          "1.因超时支付无法到账，请重新发起。\n2.每天发起支付不能超过5次，连续发起且未支付，账号可能被加入黑名单。\n3.支付通道在夜间比较忙碌，尝试多次发起，后台会为您自动切换不同支付通道。\n4.若充值成功，用户权益通常会在数分钟内到账，请刷新APP或重启。\n6.若支付成功并重启APP后依然没有到账，请联系在线客服并提供付款成功凭证截图。",
      'isOpen': false
    },
    {"question": "收到手机报毒提醒?", "answer": "本平台有主要收益为广告赞助，且保证APP安全无毒，因平台主要展示内容为色情属于特殊行业，某些杀毒软件会误报毒提醒，如遇此类提醒请忽略继续使用。", 'isOpen': false},
    {"question": "联系方式?", "answer": "商务合作TG: $tg\n官方邮箱: $email\n永久下载地址: $url", 'isOpen': false},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("常见问题"),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(16, 12, 16, 32),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            ...List.generate(
              questionList.length,
              (index) => _getItem(index),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getItem(int index) {
    final item = questionList[index];
    final isOpen = item["isOpen"] as bool;
    return InkWell(
      onTap: () {
        item["isOpen"] = !isOpen;
        setState(() {});
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                item["question"] as String,
                style: TextStyle(
                  color: Colors.white.withOpacity(0.9),
                  fontWeight: FontWeight.w500,
                  fontSize: 14,
                ),
              ),
              Spacer(),
              Transform.rotate(
                angle: isOpen ? (pi * -0.5) : 0,
                child: Icon(
                  Icons.arrow_forward_ios_rounded,
                  size: 14,
                  color: Colors.white,
                ),
              ),
            ],
          ),
          if (isOpen) ...[
            10.sizeBoxH,
            EasyRichText(
              '${item["answer"] as String}',
              defaultStyle: TextStyle(
                color: Color(0xff989898),
                fontWeight: FontWeight.w400,
                fontSize: 12,
              ),
              patternList: [
                EasyRichTextPattern(
                  targetString: tg,
                  style: TextStyle(color: AppColors.primaryHighColor),
                  recognizer: TapGestureRecognizer()
                    ..onTapDown = (details) {
                      launchUrlToWeb(tg);
                    },
                ),
                EasyRichTextPattern(
                  targetString: url,
                  style: TextStyle(color: AppColors.primaryHighColor),
                  recognizer: TapGestureRecognizer()
                    ..onTapDown = (details) {
                      launchUrlToWeb('https://$url');
                    },
                ),
              ],
            ),
          ],
          12.sizeBoxH,
          Divider(
            height: .5,
            color: Colors.black.withOpacity(.04),
          ),
          19.sizeBoxH,
        ],
      ),
    );
  }
}
